"""
@Filename:  web_driver/po/IndexPage
@Author:  liukai
@Time:  2022/11/28 0028  12:17
"""
import time

import webdriver_helper
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_helper import debugger


class IndexPage:
    """登录p2p"""

    def __init__(self, d: WebDriver):
        self.d = d

    btn_login = (By.XPATH, '//*[@id="user_head_tip"]/a[1]')
    ipt_username = (By.XPATH, '//*[@id="login-email-address"]')
    ipt_password = (By.XPATH, '//*[@id="login-password"]')
    btn_submit = (By.XPATH, '//*[@id="ajax-login-submit"]')
    msg = (By.XPATH, '//div[@class="dialog-content"]')
    msg_close = (By.XPATH, '//*[@id="fanwe_success_box"]/table/tbody/tr/td[2]/div[3]/input[1]')
    msg_close2 = (By.XPATH, '//*[@id="fanwe_error_box"]/table/tbody/tr/td[2]/div[3]/input[1]')
    def login(self, username, password):
        print("login")
        self.d.find_element(*self.btn_login).click()
        self.d.find_element(*self.ipt_username).send_keys(username)
        self.d.find_element(*self.ipt_password).send_keys(password)
        self.d.find_element(*self.btn_submit).click()
        time.sleep(1)
        text = WebDriverWait(self.d, 10).until(
            lambda x: x.find_elements(*self.msg)[-1].text.strip())
        try:
            self.d.find_element(*self.msg_close2).click()
        except Exception as e:
            self.d.find_element(*self.msg_close).click()
        return text


class Investment:
    """投资"""

    def __init__(self, driver: WebDriver):
        self.d = driver

    touzi_menu = (By.XPATH, '//*[@id="header"]/div[2]/div/ul/li[2]/a')
    investment_menu = (By.XPATH, '//*[@id="header"]/div[2]/div/ul/li[2]/div/a[2]')
    toubiao_btn = (By.XPATH, '/html/body/div[3]/div[1]/div[2]/div[1]/div[2]/div[2]/a')
    # /html/body/div[3]/div[1]/div[2]/div[1]/div[2]/div[2]/a
    #     /html/body/div[3]/div[1]/div[2]/div[3]/div[2]/div[2]/a
    ipt_num = (By.XPATH, '//*[@id="J_BIDMONEY"]')
    liji_btn = (By.XPATH, '//*[@id="tz_link"]')
    ipt_pass = (By.XPATH, '//*[@id="J_bid_password"]')
    btn_submit = (By.XPATH, '//*[@id="J_bindpassword_btn"]')
    toubiao_msg = (By.XPATH, '//*[@id="fanwe_success_box"]/table/tbody/tr/td[2]/div[2]')
    msg_close = (By.XPATH, '//*[@id="fanwe_success_box"]/table/tbody/tr/td[2]/div[3]/input[1]')

    def pay(self, number, password):
        touzi = self.d.find_element(*self.touzi_menu)
        ac = ActionChains(self.d)
        print(type(touzi.location), touzi.location)
        ac.move_by_offset(touzi.location['x'], touzi.location['y'])
        ac.perform()

        # 个人贷款
        self.d.find_element(*self.investment_menu).click()
        time.sleep(1)
        # 投标
        self.d.find_element(*self.toubiao_btn).click()
        # 输入数字
        self.d.find_element(*self.ipt_num).send_keys(number)
        self.d.find_element(*self.liji_btn).click()
        # 输入密码
        self.d.find_element(*self.ipt_pass).send_keys(password)
        self.d.find_element(*self.btn_submit).click()
        text = WebDriverWait(self.d, 10).until(
            lambda x: x.find_element(*self.toubiao_msg).text.strip())

        self.d.find_element(*self.msg_close).click()
        return text


if __name__ == '__main__':
    d = webdriver_helper.get_webdriver()
    d.maximize_window()
    d.implicitly_wait(3)
    d.get("http://47.107.116.139/fangwei/index.php")
    ind = IndexPage(d)
    text = ind.login("admin", "msjy123")

    print(text)
    if text != '成功登录':
        print("登录失败！")
    text = Investment(d).pay("100", "msjy123")
    print(text)
    if text != '投标成功！':
        print("投标失败！")
